package com.nexgo.payment.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nexgo.common.ByteUtils;
import com.nexgo.oaf.apiv3.emv.EmvCardLogEntity;
import com.nexgo.payment.R;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 电子现金明细列表适配器
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年09月4日
 */
public class EcLogListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = EcLogListAdapter.class.getSimpleName();

    private Context mContext;
    private LayoutInflater mInflater;
    private List<EmvCardLogEntity> mLogEntityList;

    public EcLogListAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public void addLogList(List<EmvCardLogEntity> logEntityList) {
        this.mLogEntityList = logEntityList;
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (mLogEntityList != null) {
            return mLogEntityList.size();
        }
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_ec_log_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder mHolder = (ViewHolder) holder;
        EmvCardLogEntity logEntity = mLogEntityList.get(position);
        //交易类型
        mHolder.tvTradeType.setText(logEntity.getServeType());
        //商户名称
        String merName = "";
        try {
            merName = new String(ByteUtils.hexString2ByteArray(logEntity.getMerName()), SysConstant.GBK);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        mHolder.tvMerchantName.setText(merName);
        //交易金额
        mHolder.tvTradeAmount.setText(StringUtils.formatAmount(logEntity.getAmt(), SysConstant.FUND_DIGITS));
        //其他金额
        mHolder.tvOtherAmount.setText(StringUtils.formatAmount(logEntity.getOtherAmt(), SysConstant.FUND_DIGITS));
        //货币代码
        mHolder.tvFuncCode.setText(logEntity.getCurCode());
        //国家代码
        mHolder.tvCountryCode.setText(logEntity.getCntCode());
        //交易计数器
        mHolder.tvTradeCount.setText(logEntity.getAtc());
        //交易时间
        mHolder.tvTradeTime.setText(StringUtils.formatDateTime(logEntity.getTransDate(), logEntity.getTransTime()));
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_trade_type)
        TextView tvTradeType;
        @BindView(R.id.tv_merchant_name)
        TextView tvMerchantName;
        @BindView(R.id.tv_trade_amount)
        TextView tvTradeAmount;
        @BindView(R.id.tv_other_amount)
        TextView tvOtherAmount;
        @BindView(R.id.tv_func_code)
        TextView tvFuncCode;
        @BindView(R.id.tv_country_code)
        TextView tvCountryCode;
        @BindView(R.id.tv_trade_count)
        TextView tvTradeCount;
        @BindView(R.id.tv_trade_time)
        TextView tvTradeTime;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
